/***************************************************************************
 *   oboe.controllers.jml.helpers.JMLButton                                *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml.helpers
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextLineMetrics;
	
	import oboe.controllers.jml.events.JMLEvent;
	import oboe.core.Controller;
	
	public class JMLButton extends Sprite
	{
		private var label:TextField;
		public var buttonColor1:int = 0xC0C0C0;
		public var buttonColor2:int = 0x909090;
		public var buttonBorderColor:int = 0x707070;
		public var textColor:int = 0xF0F0F0;
		public var textSize:int = 16;
		public var textFont:String = null;
		
		public var text:String;
		
		public static const SIG_ONCLICKname:String = "onClick****";
		
		public function JMLButton()
		{
			super();
			this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
			this.label = new TextField();
			this.addChild( this.label );
			Object(this).addCallback( MouseEvent.CLICK, onClick );
		}
		
		private function onClick():void
		{
			var pc:Controller = Object(this.parent).getController();
			if( !pc ) return;
			pc.message("onClick" + this.name);
		}
		
		private function onComplete( e:JMLEvent ):void
		{
			var box:Matrix = new Matrix();
			var width:Number = e.source.width;
			var height:Number = e.source.height;
			this.scaleX = this.scaleY = 1;
			box.createGradientBox( width, height, Math.PI/2 );
			this.graphics.lineStyle(2, this.buttonBorderColor, 1);
			this.graphics.beginGradientFill( GradientType.LINEAR, [this.buttonColor1, this.buttonColor2], [1, 1], [0, 255], box );
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();

			var tf:TextFormat = new TextFormat( this.textFont, this.textSize, this.textColor );
			this.label.defaultTextFormat = tf;
			this.label.autoSize = TextFieldAutoSize.LEFT;
			this.label.text = this.text;
			this.label.selectable = false;
			this.label.x = width/2 - this.label.width/2;
			this.label.y = height/2 - this.label.height/2;
			
			this.scrollRect = new Rectangle(0, 0, width, height );
			this.mouseChildren = false;
			this.buttonMode = true;
		}
		
	}
}